package com.powernode.controller;

import cn.hutool.core.io.FileUtil;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashSet;


@RequestMapping("/admin/file/upload")
@RestController
@Api(tags = "图片上传管理")
public class FileUploadController {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Value("${resources.url}")
    private String url;

    /**
     * 所以图片上传接口
     *
     * @param file
     * @return 返回保存到fastdfs中图片路径
     */
    @PostMapping("/element")
    @ApiOperation("图片上传管理")
    public String getCategoryPage(MultipartFile file) throws IOException {


        //获得图片扩展名
        String originalFilename = file.getOriginalFilename();
        //获得后缀
        String suffix = FileUtil.getSuffix(originalFilename);
        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), suffix, new HashSet<>());
        String fullPath = storePath.getFullPath();
        return url + fullPath;
    }
}
